/**
 * Unified helper: openUploadFromRowClick
 * -------------------------------------------------------------
 * Purpose: From a row click (outside the uploader), open the file picker of
 * a VantUploader-based component safely and consistently.
 *
 * Features:
 * - Ignores clicks originating inside the uploader and common interactive areas
 *   (more/delete icons, overlay/action-sheet/dialog/preview-cover), preventing
 *   accidental trigger of the upload picker when tapping “更多/删除/预览”等。
 * - Prefers native input[type=file] for stronger compatibility (iOS/微信),
 *   and falls back to clicking the .van-uploader__upload container.
 * - Returns boolean indicating whether it attempted to open the picker.
 */

export interface OpenOptions {
  /** Extra selectors to ignore (in addition to built-in defaults) */
  ignoreSelectors?: string[];
}

const DEFAULT_IGNORE_SELECTORS = [
  '.van-uploader',
  '.file-row__more',
  '.file-row__remove',
  '.van-action-sheet',
  '.van-action-sheet__content',
  '.van-overlay',
  '.van-cell',
  '.van-dialog',
  '.van-image-preview',
  '.van-uploader__preview-cover',
  '.file-row-cover',
];

/**
 * Try opening upload picker from a row click.
 * @param e The click/tap event
 * @param compRef Component instance or its ref.value (must expose openFilePicker or contain input[type=file])
 * @param options Optionally extend ignored selectors
 * @returns true if the picker was triggered; false otherwise
 */
export function openUploadFromRowClick(
  e: Event,
  compRef: any,
  options?: OpenOptions
): boolean {
  try {
    const target = (e as any)?.target as Element | null;
    if (!target) return false;

    // Ignore clicks initiated within the uploader root
    const withinUploader = (target as any)?.closest && (target as any).closest('.van-uploader');
    if (withinUploader) return false;

    // Ignore clicks on common interactive areas (more/delete/overlay/action-sheet/dialog/preview-cover)
    const ignoreSelectors = options?.ignoreSelectors?.length
      ? [...DEFAULT_IGNORE_SELECTORS, ...options.ignoreSelectors]
      : DEFAULT_IGNORE_SELECTORS;
    const shouldIgnore = (target as any)?.closest && (target as any).closest(ignoreSelectors.join(','));
    if (shouldIgnore) return false;

    const comp = compRef;
    // Prefer child component’s openFilePicker (with internal debouncing)
    if (comp?.openFilePicker) {
      comp.openFilePicker();
      return true;
    }

    // Fallback: trigger native input[type=file], then uploader container
    const root = comp?.$el || comp;
    const input = root?.querySelector?.('input[type="file"]');
    if (input) {
      (input as HTMLInputElement).click();
      return true;
    }
    const uploadEl = root?.querySelector?.('.van-uploader__upload');
    if (uploadEl) {
      (uploadEl as HTMLElement).click();
      return true;
    }
    return false;
  } catch (err) {
    console.warn('[openUploadFromRowClick] failed:', err);
    return false;
  }
}

/**
 * Create a ready-to-bind handler for row click.
 * Usage:
 *   const compRef = ref(null);
 *   const onRowClick = createUploadRowClickHandler(compRef);
 *   // <div @click="onRowClick">...</div>
 */
export function createUploadRowClickHandler(
  compRef: any,
  options?: OpenOptions
) {
  return (e: Event) => openUploadFromRowClick(e, compRef?.value ?? compRef, options);
}